package cn.ismylover.jsandroid;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView web_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web_view = findViewById(R.id.web_view);

        WebSettings webSettings = web_view.getSettings();
        webSettings.setJavaScriptEnabled(true);
        web_view.addJavascriptInterface(new JsToAndroid(), "AndroidFunction"); // AndroidFunction就是命名空间

        web_view.loadUrl("http://192.168.0.179:8081/");

        //使用WebViewClient设置监听并处理WebView的请求事件
        web_view.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //根据url真正去加载网页的操作
                view.loadUrl(url);
                //在当前WebView中打开网页，而不在浏览器中
                return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        web_view.loadUrl("javascript:doBack()");
    }
}


